package controller

/*
   @Title  文件名称 : employeeController.go
   @Description 描述: 服务EmployeeController

   @Author  作者: leijmdas@163.com  时间(2024-03-30 10:29:07)
   @Update  作者: leijmdas@163.com  时间(2024-03-30 10:29:07)

*/

import (
	basedto "gitee.com/ichub/gofactroy/common/ichubbase/dto"
	"gitee.com/ichub/gofactroy/common/shop/dto"
	"gitee.com/ichub/gofactroy/common/shop/esservice"
	_ "gitee.com/ichub/gofactroy/common/shop/inter"
	"gitee.com/ichub/gofactroy/common/shop/model"
	"gitee.com/ichub/gofactroy/common/shop/service"

	"encoding/json"
	"fmt"
	"github.com/gin-gonic/gin"
	"io/ioutil"
	"strconv"
)

type EmployeeController struct {
	employeeService   service.EmployeeService
	esEmployeeService esservice.ESEmployeeService
}

var InstEmployeeController EmployeeController

func (cInst *EmployeeController) Query(ctx *gin.Context) {
	defer ctx.Request.Body.Close()
	var bodyBytes []byte
	bodyBytes, err := ioutil.ReadAll(ctx.Request.Body)

	if err != nil {
		ctx.String(basedto.CODE_REQUEST_BAD, "bad request!")
		return
	}
	// 新建缓冲区并替换原有Request.body
	//ctx.Request.Body = ioutil.NopCloser(bytes.NewBuffer(bodyBytes))

	var param dto.EmployeeQueryParam
	err = json.Unmarshal(bodyBytes, &param)
	if err != nil {

		ctx.String(basedto.CODE_REQUEST_BAD, "bad request!")
		return
	}

	//ctx.String(basedto.CODE_SUCCESS, result.ToString())
	var result *dto.EmployeePageResult
	if param.EsQuery {
		result = cInst.esEmployeeService.Query(&param)
	} else {
		result = cInst.employeeService.Query(&param)
	}

	fmt.Println(result.String())
	ctx.Header("Content-Type", "application/json")
	ctx.String(basedto.CODE_SUCCESS, result.String())
}

func (cInst *EmployeeController) FindById(ctx *gin.Context) {

	sid := ctx.DefaultQuery("id", "0")

	id, err := strconv.Atoi(sid)
	if err != nil {
		ctx.String(basedto.CODE_REQUEST_BAD, "bad request!")
		return
	}
	result := cInst.employeeService.FindById(int32(id))

	fmt.Println(result.ToString())
	ctx.Header("Content-Type", "application/json")
	ctx.String(basedto.CODE_SUCCESS, result.ToString())

}

func (cInst *EmployeeController) DeleteById(ctx *gin.Context) {

	sid := ctx.DefaultQuery("id", "0")

	id, err := strconv.Atoi(sid)
	if err != nil {
		ctx.String(basedto.CODE_REQUEST_BAD, "bad request!")
		return
	}
	result := cInst.employeeService.DeleteById(int32(id))
	data, _ := json.Marshal(result)
	ctx.Header("Content-Type", "application/json")
	ctx.String(basedto.CODE_SUCCESS, string(data))

}

/*
@title     函数名称: Save
@description      : 保存接口  主键%s为nil，0新增，!=nil修改

	Save(ctx *gin.Context)

@auth      作者    :   leijmdas@163.com 时间: 2024-03-30 10:29:07
@param     输入参数名:  ctx *gin.Context
@return    返回参数名:  无
*/
func (cInst *EmployeeController) Save(ctx *gin.Context) {
	defer ctx.Request.Body.Close()
	var bodyBytes []byte
	bodyBytes, err := ioutil.ReadAll(ctx.Request.Body)
	if err != nil {
		ctx.String(basedto.CODE_REQUEST_BAD, "bad request!")
		return
	}

	// 新建缓冲区并替换原有Request.body
	//ctx.Request.Body = ioutil.NopCloser(bytes.NewBuffer(bodyBytes))
	var entity model.Employee
	err = json.Unmarshal(bodyBytes, &entity)
	if err != nil {
		ctx.String(basedto.CODE_REQUEST_BAD, "bad request!")
		return
	}
	result := cInst.employeeService.Save(&entity)

	fmt.Println(result.ToString())
	ctx.Header("Content-Type", "application/json")
	ctx.String(basedto.CODE_SUCCESS, result.ToString())

}

func (cInst *EmployeeController) UpdateNotNullProps(ctx *gin.Context) {
	defer ctx.Request.Body.Close()
	var bodyBytes []byte
	bodyBytes, err := ioutil.ReadAll(ctx.Request.Body)
	defer ctx.Request.Body.Close()
	if err != nil {
		ctx.String(basedto.CODE_REQUEST_BAD, "bad request!")
		return
	}

	// 新建缓冲区并替换原有Request.body
	//ctx.Request.Body = ioutil.NopCloser(bytes.NewBuffer(bodyBytes))
	var entity model.Employee
	err = json.Unmarshal(bodyBytes, &entity)
	if err != nil {
		ctx.String(basedto.CODE_REQUEST_BAD, "bad request!")
		return
	}
	/*result := cInst.employeeService.UpdateNotNullProps(entity.Id,   map[string]interface{})

		fmt.Println(result.ToString())
		ctx.Header("Content-Type","application/json")
	    ctx.String(basedto.CODE_SUCCESS, result.ToString())*/
}
